package com.he.service.mail;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.he.dao.MailMapper;
import com.he.pojo.Mail;
import com.he.pojo.MailBox;
import com.he.pojo.MailBoxExample;
import com.he.pojo.MailExample;
import com.he.service.mailbox.MailBoxServiceI;
@Service
public class MailServiceImpl implements MailServiceI {
	@Autowired
	MailMapper mailMapper;
	@Autowired
	MailBoxServiceI boxService;
	@Override
	public List<Mail> getPageMails(MailExample mailExample, Mail mail) {
		//如果withPage属性值为1代表要分页
		if(mail.getWithPage()==1) {
			PageHelper.startPage(mail.getPageNo(), mail.getPageSize());
		}
		return mailMapper.selectByExample(mailExample);
	}

	@Override
	public List<Mail> getNoPageMails(MailExample mailExample) {
		return mailMapper.selectByExample(mailExample);
	}

	@Override
	public Mail getMailById(int id) {
		// TODO Auto-generated method stub
		return mailMapper.selectByPrimaryKey(id);
	}
	
	@Override
	@Transactional
	public int insert(Mail mail,MailBox mailBox) {
		int i=0;
		i+=mailMapper.insertSelective(mail);
		mailBox.setSendId(mail.getSendId());
		mailBox.setSendName(mail.getSendName());
		mailBox.setReceiveId(mail.getReceiveId());
		mailBox.setReceiveName(mail.getReceiveName());
		mailBox.setMailDate(mail.getMailDate());
		mailBox.setMailId(mail.getId());
		mailBox.setMailTitle(mail.getMailTitle());
		mailBox.setSendMail((byte)1);
		mailBox.setInbox((byte)1);
		i+=boxService.insert(mailBox);
		return i;
	}

	@Override
	@Transactional
	public int update(Mail mail,MailBox mailBox) {
		mailBox.setSendId(mail.getSendId());
		mailBox.setSendName(mail.getSendName());
		mailBox.setReceiveId(mail.getReceiveId());
		mailBox.setReceiveName(mail.getReceiveName());
		mailBox.setMailDate(mail.getMailDate());
		mailBox.setMailId(mail.getId());
		mailBox.setMailTitle(mail.getMailTitle());
		mailBox.setSendMail((byte)1);
		mailBox.setInbox((byte)1);
		boxService.update(mailBox);
		return mailMapper.updateByPrimaryKeySelective(mail);
	}

	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		return mailMapper.deleteByPrimaryKey(id);
	}

	@Override
	@Transactional
	public int saveDraft(Mail mail, MailBox mailBox) {
		int i=0;
		i+=mailMapper.insertSelective(mail);
		mailBox.setSendId(mail.getSendId());
		mailBox.setSendName(mail.getSendName());
		mailBox.setReceiveId(mail.getReceiveId());
		mailBox.setReceiveName(mail.getReceiveName());
		mailBox.setMailDate(mail.getMailDate());
		mailBox.setMailId(mail.getId());
		mailBox.setMailTitle(mail.getMailTitle());
		mailBox.setDrafts((byte)1);
		i+=boxService.insert(mailBox);
		return i;
	}

	@Override
	@Transactional
	public int updateDraft(Mail mail, MailBox mailBox) {
		int i=0;
		i+=mailMapper.updateByPrimaryKeySelective(mail);
		mailBox.setDrafts((byte)0);
		mailBox.setSendMail((byte)1);
		mailBox.setInbox((byte)1);
		MailBoxExample example=new MailBoxExample();
		example.createCriteria().andMailIdEqualTo(mail.getId());
		mailBox.setId(boxService.getNoPageMailBox(example).get(0).getId());
		i+=boxService.update(mailBox);
		return i;
	}

}
